$(function () {
    var tcName = $("#userName").val();
    if (tcName != "") {
        $("#refundPeople2 option[value='" + tcName + "']").attr("selected", "selected");
    }
    if ($("#refundPeople2").val() != '') {
        $("#refundPeople2").attr('disabled', true).css('background', '#ebebe4');
    }
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
    $("#refundTime2").val(now);
    var orderList = $(".backOrders2");
    var totalFoldingPay = 0.00;
    var totalServiceActualPay = 0.00;
    var totalBackActual = 0.00;
    var totalBackFoldingPay = 0.00;
    var totalBackServiceActualPay = 0.00;
    var totalDeductionAmount = 0.00;
    var deductionAmount = 0.00;
    for (var i = 0; i < orderList.length; i++) {
        var obj = orderList[i];
        totalFoldingPay = totalFoldingPay + parseFloat($(obj).find(".foldingPay").html());
        totalServiceActualPay = totalServiceActualPay + parseFloat($(obj).find(".serviceActualPay").html());
        totalBackFoldingPay = totalBackFoldingPay + parseFloat($(obj).find(".backFoldingPay").html());
        totalBackServiceActualPay = totalBackServiceActualPay + parseFloat($(obj).find(".backServiceActualPay").html());
        totalDeductionAmount = totalDeductionAmount + parseFloat($(obj).find(".deductionAmount").html());
        totalBackActual = totalBackActual + parseFloat($(obj).find(".backActual").val());
        deductionAmount = deductionAmount + parseFloat($(obj).find(".integrals").val());
    }
    $("#totalFoldingAmount").html(totalFoldingPay);
    $("#totalRefundAmount").html(totalServiceActualPay);
    $("#totalRefundAmount1").html(totalBackActual);
    $("#totalBackFoldingPay").html(totalBackFoldingPay);
    $("#totalDeductionAmount").html(totalDeductionAmount);
    $("#totalBackServiceActualPay").html(totalBackServiceActualPay);
    $("#totalRefundAmount2").val(totalServiceActualPay);
    $("#deductionAmount").val(deductionAmount);
    $("#deductionAmount1").html(deductionAmount);
});

function clickAll() {
    $(".checkOne").prop("checked", $(".checkAll").prop("checked"));
}

function changeBackActual(obj) {
    var backAmount = parseFloat($(obj).val());
    var serviceActualPay = parseFloat($(obj).parent().parent().find(".serviceActualPay").html());
    if (backAmount) {
        if (backAmount > serviceActualPay) {
            layer.msg('退款金额不能超过实收金额！', {time: 1000, icon: 2});
            $(obj).val(serviceActualPay);
        } else {
            var backFoldingPay = parseFloat(serviceActualPay - backAmount).toFixed(2);
            $(obj).parent().parent().find(".backFoldingPay").html(backFoldingPay);
            $(obj).parent().parent().find(".backServiceActualPay").html(backFoldingPay);
        }
    } else {
        $(obj).val(0);
    }
    var orderList = $(".backOrders2");
    var totalRefundAmount1 = 0.00;
    var totalBackFoldingPay = 0.00;
    var totalBackServiceActualPay = 0.00;
    for (var i = 0; i < orderList.length; i++) {
        var obj = orderList[i];
        totalRefundAmount1 = totalRefundAmount1 + parseFloat($(obj).find(".backActual").val());
        totalBackFoldingPay = totalBackFoldingPay + parseFloat($(obj).find(".backFoldingPay").html());
        totalBackServiceActualPay = totalBackServiceActualPay + parseFloat($(obj).find(".backServiceActualPay").html());
    }
    $("#totalRefundAmount1").html(totalRefundAmount1.toFixed(2));
    $("#totalBackFoldingPay").html(totalBackFoldingPay.toFixed(2));
    $("#totalBackServiceActualPay").html(totalBackServiceActualPay.toFixed(2));
    $("#totalRefundAmount2").val(totalRefundAmount1.toFixed(2));
}

function decimalNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        obj.value = parseFloat(obj.value);
    }
}

function changeIntegrals(obj) {
    var integrals = parseFloat($(obj).val());
    var deductionAmount = parseFloat($(obj).parent().parent().find(".deductionAmount").html());
    if (!isNaN(integrals)) {
        if (integrals > deductionAmount) {
            layer.msg('退款金额不能超过实收金额！', {time: 1000, icon: 2});
            $(obj).val(deductionAmount);
        } else {
            var orderList = $(".backOrders2");
            var deductionAmounts = 0.00;
            for (var i = 0; i < orderList.length; i++) {
                var aec = orderList[i];
                deductionAmounts = deductionAmounts + parseFloat($(aec).find(".integrals").val());
            }
            $("#deductionAmount").val(deductionAmounts);
            $("#deductionAmount1").html(deductionAmounts);
        }
    } else {
        $(obj).val(0);
        var orderList = $(".backOrders2");
        var deductionAmounts = 0.00;
        for (var i = 0; i < orderList.length; i++) {
            var aec = orderList[i];
            deductionAmounts = deductionAmounts + parseFloat($(aec).find(".integrals").val());
        }
        $("#deductionAmount").val(deductionAmounts);
        $("#deductionAmount1").html(deductionAmounts);
    }
}

function getNow(s) {
    return s < 10 ? '0' + s : s;
}

var refundcount = 0;

function closesd() {
    refundcount = 0;
    parent.layer.close(parent.layer.getFrameIndex(window.name));
}

function retreatOrderAmount(obj) {  //退金额
    refundcount++;
    if (refundcount == 1) {
        $.ajax({
            url: Feng.ctxPath + "/consultant/getCustomerStatus/" + $("#customerId").val() ,
            dataType: "json",
            contentType: "application/json",
            type: "POST",
            success: function (data) {
                if (data.split(",")[0] == '复诊') {
                    alert("复诊不能退款！请重咨");
                    refundcount = 0;
                    return;
                } else if (data.split(",")[1] > 0) {
                    alert("此患者还有未结账的单，不能退款！");
                    refundcount = 0;
                    return;
                } else {
                    var backOrders = $(".backOrders2");
                    var dataList = [];
                    var getTime = $("#refundTime2").val();
                    var refundPeople = $("#refundPeople2").val();
                    if (getTime == null || getTime == "") {
                        alert("请选择退款时间");
                        refundcount = 0;
                        return false;
                    }
                    if (refundPeople == null || refundPeople == "") {
                        alert("请选择退款人");
                        refundcount = 0;
                        return false;
                    }
                    var remarks = $("#remarks2").val();
                    if (remarks == null || remarks == "") {
                        alert("请输入退款备注！");
                        refundcount = 0;
                        return false;
                    }
                    for (var i = 0; i < backOrders.length; i++) {
                        var obj = backOrders[i];
                        var deal = {};
                        deal.id2 = $(obj).find(".id2").val();
                        deal.isDo = 4;
                        deal.customerId = $("#customerId").val();
                        deal.deductionTypeName = $("#deductionTypeName").val();
                        deal.reservationsId = $("#reservationsId").val();
                        deal.dealStatus = "已开单";
                        deal.tcName = refundPeople;
                        deal.foldingPay = $(obj).find(".backFoldingPay").html();
                        deal.serviceActualPay = $(obj).find(".backActual").val();
                        deal.deductionAmount = $(obj).find(".integrals").val();
                        deal.deductionAmountyhq = $(obj).find(".inputs").val();
                        deal.remarks = remarks;
                        dataList.push(deal);
                    }
                    $.ajax({
                        url: Feng.ctxPath + "/consultant/backAllOrder1",
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(dataList),
                        type: "POST",
                        success: function (data) {
                            $(obj).attr("disabled", "true"); //设置变灰按钮
                            layer.msg('操作成功！', {time: 1000, icon: 1});
                            window.parent.location.href = Feng.ctxPath + "/consultantDetails/consultantToDetailByTab/" + $("#customerId").val() + '/2/0';
                        }
                    });
                }
            }
        });
    }
}